package com.company.oa.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.user.dto.UserDTO;
import com.company.oa.user.entity.User;
import com.company.oa.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    public Result<IPage<User>> getUserPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            User user) {
        IPage<User> userPage = userService.getUserPage(page, size, user);
        return Result.success(userPage);
    }

    /**
     * 根据ID查询用户
     */
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.failed("用户不存在");
        }
        return Result.success(user);
    }

    /**
     * 添加用户
     */
    @PostMapping
    public Result<Boolean> addUser(@RequestBody @Valid UserDTO userDTO) {
        boolean result = userService.addUser(userDTO);
        return Result.success(result);
    }

    /**
     * 更新用户
     */
    @PutMapping
    public Result<Boolean> updateUser(@RequestBody @Valid UserDTO userDTO) {
        boolean result = userService.updateUser(userDTO);
        return Result.success(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable Long id) {
        boolean result = userService.deleteUser(id);
        return Result.success(result);
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteUser(@RequestBody List<Long> ids) {
        boolean result = userService.batchDeleteUser(ids);
        return Result.success(result);
    }

    /**
     * 重置密码
     */
    @PutMapping("/{id}/reset-password")
    public Result<Boolean> resetPassword(@PathVariable Long id) {
        boolean result = userService.resetPassword(id);
        return Result.success(result);
    }

    /**
     * 修改密码
     */
    @PutMapping("/change-password")
    public Result<Boolean> changePassword(
            @RequestParam Long id,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        boolean result = userService.changePassword(id, oldPassword, newPassword);
        return Result.success(result);
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> changeStatus(
            @PathVariable Long id,
            @PathVariable Integer status) {
        boolean result = userService.changeStatus(id, status);
        return Result.success(result);
    }

    /**
     * 获取用户角色ID列表
     */
    @GetMapping("/{id}/roles")
    public Result<List<Long>> getUserRoleIds(@PathVariable Long id) {
        List<Long> roleIds = userService.getUserRoleIds(id);
        return Result.success(roleIds);
    }

    /**
     * 获取用户权限列表
     */
    @GetMapping("/{id}/permissions")
    public Result<List<String>> getUserPermissions(@PathVariable Long id) {
        List<String> permissions = userService.getUserPermissions(id);
        return Result.success(permissions);
    }
} 